-- slime_bonus
-- create by wangy
-- 道具：指定冈布奥触发奖励
-- 参数列说明：({condition, bonus, amount})
-- 参数列说明：({slime_id, bonus}) slime_id 所指定的slime，bonus固定奖励

return {
    apply = function(source, target, classId, value)
        -- 获取当前出战冈布奥是否触发奖励
        local pet = ME.user:getActivePet();
        local slimeId = pet.classId;
        if not value[slimeId] then
            return false;
        end

        -- 奖励，这里只给玩家奖励
        -- 如果奖励配置了公式，则调用公式计算
        local bonus = value[slimeId];
        if type(bonus) == "string" and
            string.startWith(bonus, "formula_") then
            local prefix = "formula_";
            local formulaName = string.sub(bonus, string.len(prefix) + 1);

            bonus = FormulaM.invoke(formulaName, ME.user);
        end

        local function percentAttribBonus(source, attribName, force)
            local value = 0;
            if "hp" == attribName or "mp" == attribName then
                -- 千分比恢复属性
                local maxAttrib = "max_" .. attribName;
                value = math.modf(source:queryAttrib(maxAttrib) * force / 1000);
            else
                -- 千分比增加属性
                -- 清空所有buff、debuff、special
                local baseAttrib = InterfaceM.calcBaseAttrib(source, attribName);

                value = math.modf(baseAttrib * force / 1000);
            end

            -- 最少加1点
            value = math.max(1, value);
            BonusM.doBonus({ 2, attribName, value, }, "apply_property");
        end

        if type(bonus[1]) == 'number' then
            -- 单个奖励
            -- 特殊处理千分比的属性奖励
            if bonus[1] == 2 and  type(bonus[2] == 'string')
                and string.startWith(bonus[2], "per_") then
                -- 一次性的千分比属性奖励
                local force = bonus[3];
                local attribName = string.sub(bonus[2], string.len("per_") + 1);
                percentAttribBonus(source, attribName, force);
            else
                BonusM.doBonus(bonus, "apply_property");

                -- 龙珠效果需要单独处理
                if bonus[2] == 1500 then
                    EventMgr.fire(event.GAIN_DRAGON_BALL, { ["num"] = bonus[3], } );
                end
            end
        else
            -- 多个奖励
            for _, arr in pairs(bonus) do
                if arr[1] == 2 and  type(arr[2] == 'string')
                    and string.startWith(arr[2], "per_") then
                    -- 一次性的千分比属性奖励
                    local force = arr[3];
                    local attribName = string.sub(arr[2], string.len("per_") + 1);
                    percentAttribBonus(source, attribName, force);
                else
                    BonusM.doBonus(arr, "apply_property");

                    -- 龙珠效果需要单独处理
                    if arr[2] == 1500 then
                        EventMgr.fire(event.GAIN_DRAGON_BALL, { ["num"] = bonus[3], } );
                    end
                end
            end
        end

        -- 如果有统计需求
        local dbase = PropertyM.query(classId, "dbase");
        if dbase["slime_use_stat"] then
            BuildingBonusM.addFieldStat(dbase["slime_use_stat"], 1);
        end
    end,
};
